package dao;

import java.math.BigDecimal;
import java.util.HashMap;

public class Customer {

    public static BigDecimal buyAppleandStrawberry(Integer appleNum, Integer strawberryNum) {

        return new Apple().price.multiply(new BigDecimal(appleNum)).add(new Strawberry().price.multiply(new BigDecimal(strawberryNum)));
    }

    public static BigDecimal buyFruits(HashMap<Fruit,Integer> fruits) {
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (Fruit fruit : fruits.keySet()) {
            Class<?> childClass  = fruit.getClass();
            try {
                java.lang.reflect.Field priceField = childClass.getDeclaredField("price");
                BigDecimal price = (BigDecimal) priceField.get(fruit);
                totalPrice = totalPrice.add(price.multiply(new BigDecimal(fruits.get(fruit))));
            } catch (NoSuchFieldException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return totalPrice;
    }

    public static BigDecimal buyFruitsWithDiscount(HashMap<Fruit,Integer> fruits) {
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (Fruit fruit : fruits.keySet()) {
            Class<?> childClass  = fruit.getClass();
            try {
                java.lang.reflect.Field priceField = childClass.getDeclaredField("price");
                BigDecimal price = (BigDecimal) priceField.get(fruit);
                java.lang.reflect.Field discountField = childClass.getDeclaredField("discount");
                BigDecimal discount = (BigDecimal) discountField.get(fruit);
                if (discount != null) {
                    totalPrice = totalPrice.add(price.multiply(new BigDecimal(fruits.get(fruit))).multiply(discount).divide(new BigDecimal(100)));
                }else {
                    totalPrice = totalPrice.add(price.multiply(new BigDecimal(fruits.get(fruit))));
                }

            } catch (NoSuchFieldException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return totalPrice;
    }
    public static BigDecimal buyFruitsWithDiscountAndCoupon(HashMap<Fruit,Integer> fruits, Coupon coupon) {
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (Fruit fruit : fruits.keySet()) {
            Class<?> childClass  = fruit.getClass();
            try {
                java.lang.reflect.Field priceField = childClass.getDeclaredField("price");
                BigDecimal price = (BigDecimal) priceField.get(fruit);
                java.lang.reflect.Field discountField = childClass.getDeclaredField("discount");
                BigDecimal discount = (BigDecimal) discountField.get(fruit);
                if (discount != null) {
                    totalPrice = totalPrice.add(price.multiply(new BigDecimal(fruits.get(fruit))).multiply(discount).divide(new BigDecimal(100)));
                }else {
                    totalPrice = totalPrice.add(price.multiply(new BigDecimal(fruits.get(fruit))));
                }

            } catch (NoSuchFieldException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

        if (coupon != null) {
            if (coupon.getUseLimit().compareTo(totalPrice)>0) {
                return totalPrice;
            }else {
                totalPrice = totalPrice.subtract(coupon.getMinusNum());
            }

        }
        return totalPrice;
    }
}
